package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.excel.ExcelUtil;
import com.gfxy.modules.items.entity.Dxry;
import com.gfxy.modules.items.mapper.DxryMapper;
import com.gfxy.modules.items.service.IDxryService;
import com.gfxy.modules.items.vo.DxryPage;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.ImportExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 单项荣誉
 * @Author: jeecg-boot
 * @Date:   2021-10-11
 * @Version: V1.0
 */
@Service
public class DxryServiceImpl extends AbstractProjectServiceImpl<DxryMapper, Dxry> implements IDxryService {
    protected int memberCellNum = 3;



    @Override
    protected void pass(String id, String isPass) {

    }

	@Override
	public List<DxryPage> getDxryMemberPageListByYear(String year) {
			return this.baseMapper.getDxryMemberPageListByYear(year);
	}
    public IPage<DxryPage> getDxryMemberPageListByYear(String year, Integer no, Integer so){
        Page<DxryPage> dxryPagePage = new Page<>(no, so);
        return dxryPagePage.setRecords(this.baseMapper.getDxryMemberPageListByYear(year));
    }


    @Override
    public Double getTotalScore(Dxry gfxyDxry) {
        return null;
    }

//    public void pass(GfxyDxry gfxyDxry) {
//        String toString = gfxyDxry.toString();
//        String isPass = gfxyDxry.toString();
//		gfxyDxryMapper.updateIsPassById(isPass, toString);
//		memberListMapper.updateIsPassByMainId(isPass, toString);
//	}

    @Override
    protected Dxry getOneProject(Row row) {
        String date = ExcelUtil.formatCell(row.getCell(0));

        String projectName = ExcelUtil.formatCell(row.getCell(1));
        String   totalscore= ExcelUtil.formatCell(row.getCell(2));
        Dxry zxjpk = new Dxry();
        zxjpk.setDate(date);
      zxjpk.setProjectName(projectName);
        zxjpk.setIsPass(0);
        //这个地方不要忘了-算总分
        zxjpk.setTotalScore(Double.valueOf(totalscore));
        return zxjpk;
    }
    @Override
    protected String[] getMemberArray(Row row) {
        String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
        return nameStrs.split(memberNameSeparator);
    }

    @Override
    public Result<?> importExcel(MultipartFile file) throws IOException {
        int SHEET_NUM = 0;
        int errorLines = 0;
        int successLines = 0;
        InputStream inputStream = file.getInputStream();
        if (!(inputStream).markSupported()) {
            inputStream = new PushbackInputStream(inputStream, 8);
        }
        Workbook book = WorkbookFactory.create(inputStream);
        Sheet sheet = book.getSheetAt(SHEET_NUM);
        List<String> errorMessage = new ArrayList<>();

        for (int i = START_ROW_NUM; i <= sheet.getLastRowNum(); i++) {
            ExcelResult excelResult = new ExcelResult(true,"");
            try{

                Row row = sheet.getRow(i);
                Dxry t = this.getOneProject(row);

                List<MemberList> memberListList = this.getMemberList(row, t, excelResult);

                for (MemberList memberList : memberListList) {
                    List<MemberList> memberLists = new ArrayList<>();
                    memberList.setRanked(1);
                    memberList.setScore(t.getTotalScore());
                    memberLists.add(memberList);
                    this.saveMain(t,memberLists);
                    t.setId(null);
                }
            } catch ( Exception e){
                e.printStackTrace();
                log.error(e.getMessage(),e);
                excelResult.setErrorMsg("excel 第 " + i+ "行数据解析出现位置异常; ");
            }

            if(excelResult.isSuccess()){
                successLines++;
            } else {
                errorLines++;
                errorMessage.add(excelResult.getErrorMsg());
            }

        }

        return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
    }
}
